//
//  ST_UIViewController+Bt.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/20.
//  Copyright © 2019 DouLei. All rights reserved.
//

import Foundation
import MJRefresh
import MBProgressHUD
import SweeterSwift



extension UIViewController{
    
    func goback_previous_controller(animated flag: Bool = true){
        
        self.goback_previous_controller(animated: flag, completion: nil)
    }
    
    func goback_previous_controller(animated flag: Bool = true,completion: (() -> Void)? = nil) {
        
        if let nav = self.navigationController,let firstVC = nav.viewControllers[safe:0]{
            
            if firstVC == self{
                
                nav.dismiss(animated: flag, completion: completion)
                
            }else{
                
                nav.popViewController(animated: flag) {
                    completion?()
                }
            }
            
        }else{
            self.dismiss(animated: flag, completion: completion)
        }
    }
    
    /**
     加载更多的样式
     */
    func load_more_option_style(tableView:UIScrollView,count:Int,pageSize:Int = 20,couldSetMJFooterClosure:(()->())) {
        
//        当前页 超过或者等于 page Size 有下一页
        if count >= pageSize{
            
            if let footer = tableView.mj_footer{
                footer.endRefreshing()
            }else{
                couldSetMJFooterClosure()
            }
        }
        else{
            tableView.mj_footer = nil
        }
    }
    
   
    
}

extension UIScrollView{
    
    /**
    上拉加载更多，不管加载多少个，当数据为0 时没有 分页
    */
   func footer_refresh(count:Int,createMJFooterClosure:(()->())){
       
       if count < 1{
           
           self.mj_footer = nil
           
       }else if self.mj_footer == nil{
           
           createMJFooterClosure()
       }
   }
    
}


//MARK: - topmost view controller
func TopmostViewController() -> UIViewController?{
    
    return UIApplication.topViewController()
    
}


//MARK: - HUD
extension UIViewController{
    
    func show_indeterminate_h_u_d(){
        
        MBProgressHUD.hide(for: self.view, animated: true)
        MBProgressHUD.showAdded(to: self.view, animated: true)
    }
    
    func hidden_indeterminate_h_u_d(){
        
        MBProgressHUD.hide(for: self.view, animated: true)
        
    }
    
    func show_indeterminate_h_u_d(text:String){
        
        MBProgressHUD.hide(for: self.view, animated: true)
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.mode = .indeterminate
        hud.label.text = text
        
    }
}


func ToastHud(text:String,view:UIView?) {
    
    main_thread_execute {
    
        guard let v = view else{
            return
        }
        MBProgressHUD.hide(for: v, animated: false)
        
        let hud = MBProgressHUD.showAdded(to: v, animated: true)
        hud.mode = .text
        hud.label.text = text
        hud.hide(animated: true, afterDelay: 1.5)
        
    }
}
